package com.ly.activiti;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author LY
 * @create 2023/11/23 14:09
 * @Description 测试类
 */
@SpringBootTest(classes = ActivitiApplication.class)
public class ActTest01 {

    @Autowired
    RepositoryService repositoryService;
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    TaskService taskService;
    @Autowired
    HistoryService historyService;

    /**
     * 部署流程
     */
    @Test
    public void test01(){
        //删除
//        repositoryService.createDeploymentQuery().list().forEach(e -> repositoryService.deleteDeployment(e.getId(), true));
        //部署
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("processes/ProcessBack.bpmn20.xml")
                .name("测试ReceiveTask流程部署")
                .deploy();
        System.out.println("deployment.getId() = "+deployment.getId());
        System.out.println("deployment.getName() = "+deployment.getName());
        startProcess();
    }

    /**
     * 启动流程
     */
    @Test
    public void startProcess(){
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("ProcessBack");
        System.out.println("processInstance.getId() = "+processInstance.getId());
        System.out.println("processInstance.getName() = "+processInstance.getName());
        System.out.println("processInstance.getProcessInstanceId() = "+processInstance.getProcessInstanceId());
    }

    /**
     * 候选人
     */
    @Test
    public void testCandidate(){
        System.out.println("*************查询任务候选人************");
        List<Task> tasks = taskService.createTaskQuery().taskCandidateUser("zs").list();
        tasks.stream().forEach(task -> {
            System.out.println("task.getId() = "+task.getId());
            System.out.println("task.getName() = "+task.getName());
            System.out.println("task.getAssignee() = "+task.getAssignee());
            //任务拾取：把任务委派给指定的人审核执行
            taskService.claim(task.getId(), "ww");
        });
    }

    /**
     * 执行任务
     */
    @Test
    public void testComplate(){
        List<Task> tasks = taskService.createTaskQuery().taskAssignee("ww").list();
        tasks.stream().forEach(task -> {
            System.out.println("task.getId() = "+task.getId());
            System.out.println("task.getName() = "+task.getName());
            System.out.println("task.getAssignee() = "+task.getAssignee());
            //任务执行
            taskService.complete(task.getId());
        });
    }

    /**
     * 执行单个任务
     */
    @Test
    public void testComplateTask(){
        Task task = taskService.createTaskQuery().singleResult();
        Map<String, Object> variables = new HashMap<>();
        variables.put("hrtype",1);
        taskService.complete(task.getId(),variables);
//        taskService.complete(task.getId());
    }

    /**
     * 触发
     */
    @Test
    public void testTigger(){
        runtimeService.trigger("177502");
    }

    @Test
    public void testTigger2(){
        String processInstaceId = "177502";
        List<HistoricActivityInstance> historicActivityInstances = historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstaceId).activityType("receiveTask").list();
        historicActivityInstances.stream().forEach(historicActivityInstance -> {
            System.out.println("historicActivityInstance.getId() = " + historicActivityInstance.getId());
            System.out.println("historicActivityInstance.getActivityId() = " + historicActivityInstance.getActivityId());
            Execution execution = runtimeService.createExecutionQuery().activityId(historicActivityInstance.getActivityId()).singleResult();
            runtimeService.trigger(execution.getId());
        });
    }

}
